package com.ludomancy.spaceopera.entities;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Stroke;
import com.ludomancy.spaceopera.BasicEntity;
import com.ludomancy.spaceopera.Game;
import com.ludomancy.spaceopera.InventoryItem;
import com.ludomancy.spaceopera.Player;
import com.ludomancy.spaceopera.SpaceWorld;

public class PullerItem extends InventoryItem
{
	private Star grabbed = null;
	private Point dist = new Point();
	
	public PullerItem()
	{
		super(TYPE_PULLER);
		desc = "Star Hauler (TM): Pull stars. Move them, swing them, build pretty patterns with worlds.";
		cost = 150;
	}
	
	public void paintPlayerUnderlay(Graphics g, int x, int y)
	{
		if(grabbed != null)
		{
			Graphics2D g2D = (Graphics2D)g;
			Stroke s = g2D.getStroke();
			g2D.setStroke(new BasicStroke(2.0f));
			g.setColor(Color.WHITE);
			Point me = Game.game.defaultProjector.worldToScreen(Game.game.player.pos);
			Point it = Game.game.defaultProjector.worldToScreen(grabbed.pos);
			g.drawLine(me.x, me.y, it.x, it.y);
			g2D.setStroke(s);
		}
	}
		
	public boolean update()
	{
		if(grabbed != null)
		{
			grabbed.pos.setLocation(Game.game.player.pos.x + dist.x, Game.game.player.pos.y + dist.y);
		}
		return false;
	}
	
	public boolean interact(BasicEntity e)
	{
		if(e instanceof Star)
		{
			grabbed = (Star)e;
			dist.setLocation(e.pos.x - Game.game.player.pos.x, e.pos.y - Game.game.player.pos.y);
			return true;
		}
		return false;
	}
	
	public String getInteractionVerbFor(BasicEntity e)
	{
		if(e instanceof Star)
		{
			return "Pull!";
		}
		return super.getInteractionVerbFor(e);
	}

	public void update(Player player, boolean activated)
	{
		if(!activated)
		{
			grabbed = null;
		}
	}

	public boolean canBeUsed()
	{
		return Game.game.currentWorld instanceof SpaceWorld;
	}
}
